package elitech.vietnam.myfashion.extend;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;

public class MyHorizontalScrollView extends HorizontalScrollView{

	private Runnable scrollerTask;
	private int initialPosition;

	private int newCheck = 10;

	public interface OnScrollStoppedListener{
	    void onScrollStopped();
	}

	private OnScrollStoppedListener onScrollStoppedListener;
	private OnInterceptTouchEvent interceptTouchEvent;

	public MyHorizontalScrollView(final Context context, AttributeSet attrs) {
	    super(context, attrs);

	    scrollerTask = new Runnable() {
	        public void run() {
	            int newPosition = getScrollY();
	            if(initialPosition - newPosition == 0){//has stopped
	                if(onScrollStoppedListener!=null){
	                    onScrollStoppedListener.onScrollStopped();
	                }
	            }else{
	                initialPosition = getScrollY();
	                MyHorizontalScrollView.this.postDelayed(scrollerTask, newCheck);
	            }
	        }
	    };
	}

	public void setOnScrollStoppedListener(MyHorizontalScrollView.OnScrollStoppedListener listener){
	    onScrollStoppedListener = listener;
	}

	public void startScrollerTask(){
	    initialPosition = getScrollY();
	    MyHorizontalScrollView.this.postDelayed(scrollerTask, newCheck);
	}
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		if (interceptTouchEvent != null)
			interceptTouchEvent.onIntercept(ev.getAction());
		return super.onInterceptTouchEvent(ev);
	}
	
	public void setOnInterceptTouchEvent(OnInterceptTouchEvent e) {
		interceptTouchEvent = e;
	}
	
	public interface OnInterceptTouchEvent {
		public void onIntercept(int touchAction);
	}
}
